<!DOCTYPE html>
<html>
    <head>
        <title>Test MediaKeys lifetime</title>
        <script src="encrypted-media-utils.js"></script>
        <script src="../../resources/testharness.js"></script>
        <script src="../../resources/testharnessreport.js"></script>
    </head>
    <body>
        <script>
            async_test(function(test)
            {
                // Create a MediaKeys object and free immediately.
                navigator.requestMediaKeySystemAccess('org.w3.clearkey', getSimpleConfiguration()).then(function(access) {
                    return access.createMediaKeys();
                }).then(function(result) {
                    // Do nothing with the created object
                }).then(function(result) {
                    // No way to verify that MediaKeys object is actually
                    // collected, but make sure it doesn't crash.
                    return createGCPromise();
                }).then(function(result) {
                    test.done();
                }).catch(function(error) {
                    forceTestFailureFromPromise(test, error);
                });
            }, 'Creating and destroying MediaKeys does not crash');

            async_test(function(test)
            {
                var mediaKeys;
                navigator.requestMediaKeySystemAccess('org.w3.clearkey', getSimpleConfiguration()).then(function(access) {
                    return access.createMediaKeys();
                }).then(function(result) {
                    mediaKeys = result;
                    return createGCPromise();
                }).then(function(result) {
                    // Check that the object still exists.
                    assert_equals(typeof mediaKeys.createSession, 'function');
                    mediaKeys = null;

                    // Now that the reference is dropped, it should be
                    // collected. No way to verify that it is actually
                    // collected, but make sure it doesn't crash.
                    return createGCPromise();
                }).then(function(result) {
                    test.done();
                }).catch(function(error) {
                    forceTestFailureFromPromise(test, error);
                });
            }, 'MediaKeys is not collected as long as we have a reference');
        </script>
    </body>
</html>
